import { Component, EventEmitter, Output } from '@angular/core';
import { FormGroup } from '@angular/forms';
import { FormBuilderService } from 'src/components/utils/form-builder.service';
import { ObjectFieldSchemaSection } from 'src/components/utils/json-schema';
import { JsonSchemaService } from 'src/components/utils/json-schema.service';
import { AlgoUnitMetadata } from '../../algo-unit-metadata';

@Component({
  selector: 'algo-unit-setting-sidebar',
  templateUrl: './algo-unit-setting-sidebar.component.html',
  styleUrl: './algo-unit-setting-sidebar.component.scss'
})
export class AlgoUnitSettingSidebarComponent {
  sidebarVisible: boolean = false;
  cfg?: AlgoUnitMetadata;
  @Output() hide: EventEmitter<AlgoUnitMetadata> = new EventEmitter();
  schema?: ObjectFieldSchemaSection;
  metaFormGroup?: FormGroup;

  constructor(private schemaService: JsonSchemaService,
    private formService: FormBuilderService) {
  }

  onEdit(cfg: AlgoUnitMetadata) {
    this.cfg = cfg;
    const { Id, ...other } = cfg;
    const schema = this.schemaService.Schema("Meta", other);
    this.schema = schema;
    const metaFromGroup = this.formService.buildForm(schema);
    metaFromGroup.setValue(other);
    this.metaFormGroup = metaFromGroup;
    this.sidebarVisible = true;
  }

  onHide() {
  }
}
